#include "GERMIndex.h"
#include "GERM2Index.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"
#include "../Utilities/Convert.h"

GERMIndex::GERMIndex(){

		mProcessor = new GERMProcessor();

		isGetData = false;

		mLayout = createMainLayout(getStringNotIterative(MAIN_TEXT_START + 3), getStringNotIterative(L_SELECT), " ", getStringNotIterative(L_BACK));

		mTitle = (Label*)mLayout->getChildren()[0];

		mItems = (ListBox*)mLayout->getChildren()[1];

		mSoftkey = (Widget*)mLayout->getChildren()[2];

		this->setMain(mLayout);
}

GERMIndex::~GERMIndex(){
	delete mLayout;
}

void GERMIndex::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->show("LRESIndex");
			break;
		case MAK_FIRE:
		case MAK_SOFTLEFT:
			GERM2Index* screen= (GERM2Index*)APPCONTROLLER->getScreen("GERM2Index");
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
			screen->setIsGetData(false);
			screen->setInput(mCompanyid, widget->getId(), widget->getCompanyName());
			APPCONTROLLER->show("GERM2Index");
			break;
	}
}

void GERMIndex::itemReceived(GERMItem& item){

	LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);
	String t = getStringIterative(L_DIGITS) + " " + item.digit;
	newItem->setCompanyName(t);
	newItem->setCompanyFont(APPCONTROLLER->getFont("default"));

	newItem->setDrawDate(item.name);
	newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

	newItem->setId(item.digit);
	//newItem->setSmsCnt(item.smscnt);
	newItem->setShortCode(item.id);
	//newItem->setIsOod(item.outofdate);

	newItem->setSkin(APPCONTROLLER->getSkin("default"));
	//newItem->addWidgetListener(this);

	mItems->setSelectedIndex(0);
	mItems->getChildren()[0]->setSelected(true);
}

void GERMIndex::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void GERMIndex::show(){
	if (!isGetData){
		if(mItems->getChildren()[0] != NULL){
			mItems->setSelectedIndex(0);
		}
		mItems->clear();
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		String url = "http://203.162.70.245:8314/WebService.asmx?WSDL";
		mProcessor->setCompanyId(mCompanyid);
		mProcessor->addRequest(url, this);
	}else if(mItems->getChildren()[0] != NULL) {
		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}
	Screen::show();
}

void GERMIndex::processingComplete(){
	isGetData = true;
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(" ");
	l->update();
	lprintfln("process GERM complete");
}

void GERMIndex::pointerReleaseEvent(MAPoint2d point){

}

void GERMIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void GERMIndex::setCompanyid(String& input, String& value){
	mCompanyid = input;
	mTitle->setCaption(value);
	mTitle->update();
}

void GERMIndex::setIsGetData(bool value){
	isGetData = value;
}
//http://203.162.70.245:8314/WebService.asmx?WSDL
